package com.itboye.hutouben.activity.qianbao;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.BankCodeBean;
import com.itboye.hutouben.presenter.DingDanPresenter;
import com.itboye.hutouben.presenter.SysTemMessgelPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的账户
 */
public class MyZhangHuActivity extends BaseActivity implements Observer {
    ImageView zhanghu_icon;
    ImageView zhanghu_add;
    ZhangHuAdapter adapter;
    ListView zhanghu_listView;
    private List<BankCodeBean> list;
    SysTemMessgelPresenter presenter;
    DingDanPresenter dingDanPresenter;

    String type;
    String tid;//删除的是哪一条
    private Dialog dialog;// 自定义Dialog
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_zhang_hu);
        list = new ArrayList<BankCodeBean>();
        type=getIntent().getStringExtra("type");
        presenter = new SysTemMessgelPresenter(this);
        dingDanPresenter=new DingDanPresenter(this);
        showProgressDialog("加载银行卡中...",true);
        presenter.onZhangHaoList(IsUtilUid.isUid());
        showExtraDialog();
        // 当添加银行卡成功之后
        IntentFilter filter1 = new IntentFilter("tianjia.exit");
        registerReceiver(exitBroadcastReceiver, filter1);

        zhanghu_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if(type.equals("tixian")){//体现界面跳转过来的时候返回并带数据
                    Intent data = new Intent();
                    data.putExtra("choose", list.get(position).getAccount());
                    data.putExtra("acctivty", list.get(position).getId());
                    setResult(102, data);
                    finish();
                }
            }
        });
        //长按
        zhanghu_listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                tid = list.get(position).getId();
                dialog.show();
                return true;
            }
        });

    }
    BroadcastReceiver exitBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            presenter.onZhangHaoList(IsUtilUid.isUid());
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitBroadcastReceiver);
    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.zhanghu_icon:
                finish();
                break;
            case R.id.zhanghu_add:// 添加银行卡
                intent = new Intent(this, AddBankCardActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.ZhangHaoList_success) {
                list = (List<BankCodeBean>) entity.getData();
                adapter = new ZhangHuAdapter(list, MyZhangHuActivity.this);
                zhanghu_listView.setAdapter(adapter);
            }
            if (entity.getEventType() == presenter.ZhangHaoList_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == DingDanPresenter.delete_zhanghao_success) {
                ByAlert.alert(entity.getData());
                presenter.onZhangHaoList(IsUtilUid.isUid());
                adapter.notifyDataSetChanged();
            }
        }
        try{
            dialog.dismiss();
            closeProgressDialog();
        }catch (Exception ex){

        }
    }

    /**
     * 删除Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(this, R.style.rel_dialog);
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_delete, null);
        dialog.setContentView(view);

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        TextView dialog_conext = (TextView) view.findViewById(R.id.dialog_conext);
        user_title.setText("删除账户");
        dialog_conext.setText("确定删除该账户？");

        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                showProgressDialog("删除中,请稍后", true);
                dingDanPresenter.deleteZhangHao(IsUtilUid.isUid(),tid);

            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }

    class ZhangHuAdapter extends BaseAdapter{
        List<BankCodeBean> bean;
        Context context;
        DingDanPresenter dingDanPresenter;

        public ZhangHuAdapter(List<BankCodeBean> bean,
                              Context context) {
            this.context = context;
            this.bean = bean;

        }

        @Override
        public int getCount() {
            return bean.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoudr houdr;
            if (convertView == null) {
                houdr = new ViewHoudr();
                convertView = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.item_zhnghu, null);
                houdr.card_type = (TextView) convertView.findViewById(R.id.card_type);
                houdr.card_name = (TextView) convertView.findViewById(R.id.card_name);
                houdr.card_code = (TextView) convertView.findViewById(R.id.card_code);
                houdr.card_img = (ImageView) convertView.findViewById(R.id.card_img);
                houdr.linitem = (LinearLayout) convertView.findViewById(R.id.linitem);

                convertView.setTag(houdr);
            } else {
                houdr = (ViewHoudr) convertView.getTag();
            }

            houdr.card_name.setText(bean.get(position).getExtra());
            if (bean.get(position).getAccount_type().equals("6179")) {
                houdr.card_img.setBackgroundResource(R.drawable.zhifubao);
                houdr.linitem.setBackgroundResource(R.drawable.alipay);
            } else {
               // houdr.card_img.setBackgroundResource(R.drawable.yinhangkazhifu);


                String code=bean.get(position).getExtra();//银行卡类型

                if(code.equals("工商银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.gongshang_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.gongshang_bank_bj);
                }else if(code.equals("北京银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.beijing_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.beijing_bank_bj);
                }else if(code.equals("广发银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.guangzhou_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.guangzhou_bank_bj);
                }else if(code.equals("广州农商银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.guangzhounongshang_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.guangzhounongshang_bank_bj);
                }else if(code.equals("花旗银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.huaqi_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.huaqi_bank_bj);
                }else if(code.equals("华夏银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.huaxia_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.huaxia_bank_bj);
                }else if(code.equals("建设银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.jianshe_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.jianshe_bank_bj);
                }else if(code.equals("交通银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.jiaotong_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.jiaotong_bank_bj);
                }else if(code.equals("民生银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.minsheng_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.minsheng_bank_bj);
                }else if(code.equals("农业银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.nongye_bankl);
                    houdr.linitem.setBackgroundResource(R.drawable.nongye_bankl_bj);
                }else if(code.equals("平安银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.pingan_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.pingan_bank_bj);
                }else if(code.equals("浦发银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.pufa_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.pufa_bank_bj);
                }else if(code.equals("深圳发展银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.shenzhenfazhan_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.shenzhenfazhan_bank_bj);
                }else if(code.equals("兴业银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.xingye_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.xingye_bank_bj);
                }else if(code.equals("邮政储蓄银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.youzheng_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.youzheng_bank_bj);
                }else if(code.equals("招商银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.zhaoshang_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.zhaoshang_bank_bj);
                }else if(code.equals("中国银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.zhongguo_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.zhongguo_bank_bj);
                }else if(code.equals("中兴银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.zhongxin_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.bank);
                }else if(code.equals("光大银行")){
                    houdr.card_img.setBackgroundResource(R.drawable.guangda_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.guangda_bank_bj);
                }else{
                    houdr.card_img.setBackgroundResource(R.drawable.zhaoshang_bank);
                    houdr.linitem.setBackgroundResource(R.drawable.bank);
                }
            }

        //    houdr.MessgeTitle.setText(bean.get(position).getName());
            String name=bean.get(position).getAccount();//银行卡号
            if(name.length()>4){
                String s=name.substring(name.length()-4, name.length());//后四位
                String ss=name.substring(0, name.length()-4);//后四位
                StringBuffer sb=new StringBuffer();
                for (int i=0;i<ss.length();i++){
                    sb.append("*");
                }
                houdr.card_code.setText(sb+s);
            }else{
                houdr.card_code.setText(bean.get(position).getAccount());
            }
            return convertView;
        }

        private class ViewHoudr {
            private TextView card_type;//类型
            private TextView card_name;
            ImageView card_img;
            private TextView card_code;
            LinearLayout linitem;
        }
    }
}
